package cn.angis.biz.flow.controller;

import cn.angis.biz.flow.dto.input.LeaveInput;
import cn.angis.biz.flow.dto.output.LeaveOutput;
import cn.angis.biz.flow.dtomapper.LeaveStruct;
import cn.angis.biz.flow.model.Leave;
import cn.angis.biz.flow.service.LeaveService;
import cn.angis.common.annotation.SLog;
import cn.angis.common.constant.DatePattern;
import cn.angis.common.model.R;
import cn.angis.db.controller.BaseController;
import cn.angis.db.model.PageModel;
import cn.angis.db.model.PageOfResult;
import com.alibaba.excel.EasyExcel;
import org.dromara.warm.flow.ui.controller.WarmFlowController;
import org.noear.snack.core.utils.DateUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.core.handle.DownloadedFile;
import org.noear.solon.data.annotation.Transaction;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotEmpty;
import org.noear.solon.validation.annotation.Validated;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* OA 请假申请表前端控制器
* @author angis.cn
* @Date 2025-06-14
*/
@SLog("OA 请假申请表")
@Mapping("/biz/flow/leave/")
@Controller
public class LeaveController extends BaseController<LeaveService, Leave> {

    /**
    * 分页查询
    * @param leaveInput
    * @param pageModel
    * @return R<PageOfResult<LeaveOutput>>
    * @Date 2025-06-14
    */
    @SLog
    @Get
    @Mapping("page")
    public R<PageOfResult<Leave>> page(LeaveInput leaveInput, PageModel pageModel) {
        return success(pageListByEntity(leaveInput, pageModel));
    }

    @Mapping("export")
    @Post
    public DownloadedFile export(LeaveInput leaveInput) throws IOException {
        List<LeaveOutput> list = baseService.leaveList(leaveInput);
        File file = newFile( DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN) + ".xlsx");
        EasyExcel.write(file, LeaveOutput.class).sheet("请假申请数据").doWrite(list);

        return new DownloadedFile(file, "请假申请数据" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }

    @Mapping("{id}")
    @Get
    public R<LeaveOutput> get(String id) {
        LeaveOutput leaveOutput = LeaveStruct.INSTANCE.toOutput(getById(id));
        return success(leaveOutput);
    }

    /**
    * 保存
    * @param leaveInput
    * @return R<Boolean>
    * @Date 2025-06-14
    */
    @SLog
    @Post
    @Mapping("save")
    public R<Integer> save(@Validated LeaveInput leaveInput) {
//    Leave leave = LeaveStruct.INSTANCE.toLeave(leaveInput);
        return success(baseService.insert(leaveInput));
    }

    /**
    * 修改
    * @param leaveInput
    * @return R<Boolean>
    * @Date 2025-06-14
    */
    @SLog
    @Post
    @Mapping("update")
    public R<Boolean> update(@Validated LeaveInput leaveInput) {
        Leave leave = LeaveStruct.INSTANCE.toLeave(leaveInput);
        return super.update(leave);
    }

    /**
    * 删除
    * @param id
    * @return R<Boolean>
    * @Date 2025-06-14
    */
    @SLog
    @Post
    @Mapping("delete")
    public R<Boolean> delete(@NotBlank String id) {
        return super.delete(id);
    }

    /**
    * 批量删除
    * @param ids
    * @return R<Boolean>
    * @Date 2025-06-14
    */
    @SLog
    @Post
    @Mapping("deleteBatch")
    public R<Boolean> deleteBatch(@NotEmpty List<String> ids) {
        return super.deleteBatch(ids);
    }

    @Mapping("submit")
    @Get
    public R<Integer> submit(LeaveInput leaveInput) {
        return success(baseService.submit(leaveInput));
    }

    @Mapping("handle")
    @Post
    public R<Integer> handle(LeaveInput leaveInput) {
        return success(baseService.handle(leaveInput));
    }

    @Mapping("rejectLast")
    @Post
    @Transaction
    public R<Integer> rejectLast(LeaveInput leaveInput) {
        return success(baseService.rejectLast(leaveInput));
    }

    @Mapping("taskBack")
    @Post
    public R<Integer> taskBack(LeaveInput leaveInput) {
        return success(baseService.taskBack(leaveInput));
    }

    @Mapping("revoke/{id}")
    @Get
    public R<Integer> revoke(String id) {
        return success(baseService.revoke(id));
    }

    @Mapping("taskBackByInsId/{id}")
    @Get
    public R<Integer> taskBackByInsId(String id) {
        return success(baseService.taskBackByInsId(id));
    }

    @Mapping("termination")
    @Post
    public R<Integer> termination(Leave testLeave) {
        return success(baseService.termination(testLeave));
    }

}
